package person

import entity.Address
import entity.Person

class PersonController {

    def index() {}

    def createPerson() {}

    def createPersonAction() {
        GregorianCalendar date = new GregorianCalendar
                (params.date_year as int, (params.date_month as int) - 1, params.date_day as int)
        def person = new Person(params)

        person.date = date.getTime()
        def address = new Address(params)
        person.address = address//set.address

        //  person.id = params.id as long//задаем id

        if (Person.get(params.id)) {

            def p = Person?.get(params.id as long)
            p.name = (params.name as String)
            p.age = (params.age as int)
            p.date = (params.date as Date)
            p.address.city = (params.city as String)
            p.address.street = (params.street as String)
            p.address.number = (params.number as String)
            p.save()
        } else
            person.save()

        redirect action: "createPerson"
    }

    def deletePerson() {
        Person person = Person.get(params.id as long);// получаем person по id
        person.delete(flush: true)
        redirect action: "createPerson"
    }

    def changePerson() {
        Person person = Person.get(params.id as long);
        render view: 'createPerson', model: [person: person]//перенаправляем на форму которая есть

    }

    def show() {
        List<Person> persons = Person.findAll("from Person",[offset:(params.offset?:0)  as int,max:5])
        println(persons)
        println(Person.count())
        println(params.offset)
        [persons: persons, personCount:Person.count() ,offset:(params.offset?:0)]//send map with personList to pages.gsp

    }

    def showAddress() {
        List<Address> addressList = Address.findAll()
        [addressList: addressList]
    }


}
